package com.ailesson.video.api;


import com.ailesson.common.reply.PageResult;
import com.ailesson.common.reply.R;
import com.ailesson.video.dto.CourseDto;
import com.ailesson.video.model.reply.CourseReply;
import com.ailesson.video.model.req.CourseQueryReq;
import com.ailesson.video.service.CourseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "课程api")
@RestController
@RequestMapping("/api/video/course")
public class CourseApiController {

    @Autowired
    private CourseService courseService;

    @Operation(summary = "根据关键字查询课程")
    @PostMapping("/findByKeyword/{keyword}")
    public R<List<CourseDto>> findByKeyword(@PathVariable("keyword") String keyword){
        return R.success(courseService.findByKeyword(keyword));
    }

    @Operation(summary ="根据ID查询课程")
    @GetMapping("/getInfo/{courseId}")
    public R<CourseDto> getById(
            @Parameter(description = "课程ID", required = true)
            @PathVariable Long courseId){
        CourseReply course = courseService.info(courseId,false);
        if (course != null){
            CourseDto dto = new CourseDto();
            BeanUtils.copyProperties(course,dto);
            return R.success(dto);
        }
        return R.success();
    }

    @Operation(summary ="根据课程分类查询课程列表")
    @GetMapping("/{subjectParentId}/{page}/{limit}")
    public R findPageCourse(@Parameter(name = "subjectParentId",description = "课程一级分类ID", required = true)
                                 @PathVariable Long subjectParentId,
                                 @Parameter(name = "page", description = "当前页码", required = true)
                                 @PathVariable Integer page,
                                 @Parameter(name = "limit", description = "每页记录数", required = true)
                                 @PathVariable Integer limit) {
        //封装条件
        CourseQueryReq queryReq = new CourseQueryReq();
        queryReq.setPageNo(page);
        queryReq.setPageSize(limit);
        queryReq.setSubjectParentId(subjectParentId);
        PageResult pageResult = courseService.queryPage(queryReq);
        return R.success(pageResult);
    }

    @Operation(summary ="根据课程id查询课程详情")
    @GetMapping("/getInfoDetail/{courseId}")
    public R getInfo(@PathVariable Long courseId) {
        Map<String,Object> map = courseService.getInfoById(courseId);
        return R.success(map);
    }
}
